package com.spring_mysql_redis.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spring_mysql_redis.annotation.CurrentUser;
import com.spring_mysql_redis.annotation.UserLoginToken;
import com.spring_mysql_redis.entity.Dynamic;
import com.spring_mysql_redis.entity.UserDynamic;
import com.spring_mysql_redis.entity.Users;
import com.spring_mysql_redis.model.ResultFormat;
import com.spring_mysql_redis.model.ResultFormatPaging;
import com.spring_mysql_redis.service.DynamicService;
import com.spring_mysql_redis.utils.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author
 */
@Api(tags = "动态管理")
@RestController
@RequestMapping("/dynamic")
public class DynamicController {

    private Logger logger = LoggerFactory.getLogger(DynamicController.class);

    @Autowired
    private DynamicService dynamicService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private FilesUtils filesUtils;

    /**
     * 分页全查询用户信息
     *
     * @return
     */
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "Integer", defaultValue = "1"),
            @ApiImplicitParam(name = "limit", value = "每页记录数", required = true, dataType = "Integer", defaultValue = "5")
    })

    /**
     * 全查询分类信息
     *
     * @return
     */
    @ApiOperation("user+dynamic联合查询全部动态接口")
    @GetMapping("/lists")
    public Page<UserDynamic> pgUserDynamicsList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        logger.info("-------------------进入用户查询controller层--------------------------");
        Page<UserDynamic> iPage = new Page<UserDynamic>(page, limit);
        return dynamicService.selectUserDynamicList(iPage);

    }

    /**
     * 全查询分类信息
     *
     * @return
     */
    @ApiOperation("根据ID查询用户user+dynamic全部动态接口")
    @GetMapping("/oneLists")
    public Page<UserDynamic> pgOneUserDynamicsList(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit,@CurrentUser Users user) {
        logger.info("-------------------进入用户查询controller层--------------------------");
        Page<UserDynamic> iPage = new Page<UserDynamic>(page, limit);
        return dynamicService.selectOneUserDynamicList(iPage,user.getId());
    }
    /**
     * 全查询分类信息
     *
     * @return
     */
    @ApiOperation("查询全部动态接口")
    @GetMapping("/list")
    public ResultFormatPaging pgDynamicsList(Dynamic dynamic, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        logger.info("-------------------进入用户查询controller层--------------------------");
        logger.info("dynamic={}", dynamic);
        IPage<Dynamic> dynamicIPage = dynamicService.selectDynamicList(dynamic, page, limit);
        List<Dynamic> dynamicList = dynamicIPage.getRecords();
        logger.info("dynamicList={}", dynamicList);
        return ResultPagingUtil.pagingSuccess(0, dynamicIPage.getTotal(), dynamicList);

    }


    /**
     * 查询动态信息
     *
     * @return
     */
    @ApiOperation("查询动态接口")
    @GetMapping("/select")
    public ResultFormat pgDDynamicOne(Dynamic dynamic, @CurrentUser Users user) {
        logger.info("-------------------进入用户查询controller层--------------------------");
        logger.info("dynamic={}", dynamic);
        Dynamic oneDynamic = dynamicService.selectDynamicOne(dynamic.getId(), user);
        logger.info("dynamicList={}", oneDynamic);
        return ResultUtil.success();
    }


    /**
     * 添加动态接口
     *
     * @param dynamic
     * @return
     */
    @ApiOperation("添加动态接口")
    @PostMapping("/save")
    @ResponseBody
    @UserLoginToken
    public ResultFormat savePgSorts(@RequestBody Dynamic dynamic, @CurrentUser Users user) {
        dynamic.setUserId(user.getId());
        System.out.println(dynamic);
        dynamic.setCreateTime(TimeUtitl.dateTime());
        ResultFormat insert = dynamicService.saveDynamic(dynamic);
        if (insert.getCode() == 200) {
            logger.info("添加成功");
            return ResultUtil.success();
        }
        if (insert.getCode() == 100) {
            logger.info("异常错误");
            return ResultUtil.error(insert.getCode(), insert.getMsg());
        }
        return ResultUtil.error(101, "上传超时");
    }


    /**
     * 修改用户信息
     *
     * @param dynamic
     * @return
     */
    @ApiOperation("修改动态接口")
    @PutMapping("/update")
    @ResponseBody
    public ResultFormat updatePgSorts(Dynamic dynamic) {

        dynamic.getCreateTime();

        Integer insert = dynamicService.updateDynamic(dynamic);

        if (insert == 101) {
            logger.info("异常错误");
            return ResultUtil.error(101, "异常错误");
        }
        if (insert == 200) {
            logger.info("修改成功");
            return ResultUtil.success();
        }

            return ResultUtil.error(100, "修改失败");

    }


    /**
     * 修改用户状态
     *
     * @param dynamic
     * @return
     */
    @ApiOperation("删除动态接口")
    @PutMapping("/delete")
    @UserLoginToken
    public ResultFormat deletePgSorts(@RequestBody Dynamic dynamic, @CurrentUser Users user) {

        logger.info("dynamic=={}", dynamic.getId());
        Integer flag = dynamicService.deleteDynamicId(dynamic.getId(),user.getId());
        System.out.println(flag+"--------------------------------------------------------------------------------");
        if (flag == 200) {
            logger.info("删除成功");
            return ResultUtil.success();
        } else {
            logger.info("删除失败");
            return ResultUtil.error(100, "删除失败");
        }
    }
    @ApiOperation("动态图片接口")
    @PostMapping("/img")
    @ResponseBody
    @UserLoginToken
    public ResultFormat imgPgSorts(@RequestParam("file") MultipartFile file, @CurrentUser Users user) {

        return filesUtils.imgPgSorts(file , user.getId());
    }
}
